package com.yidu.hbatsft.action;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.yidu.hbatsft.domain.AtsEmpLeader;
import com.yidu.hbatsft.domain.AtsEmpLeaderVo;
import com.yidu.hbatsft.service.AtsEmpLeaderService;
import com.yidu.utils.Message;
import com.yidu.utils.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 领导控制器
 */
@Controller
@RequestMapping("AtsEmpleader")
public class AtsEmpLeaderController {

    @Resource
    AtsEmpLeaderService leaderService;

    @RequestMapping("findAll")
    @ResponseBody
    public Map<String,Object> findAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize,String leaderName){

        Map<String,Object> map = new HashMap<String,Object>();

        System.out.println(leaderName+"===========================qeqeqeqe");
        List<AtsEmpLeader> ats = leaderService.findAll(pageNum,pageSize,leaderName);

        int cout = leaderService.findCout();

        System.out.println("cout = "+cout);
        map.put("code",0);
        map.put("msg","");
        map.put("count",cout);
        map.put("data",ats);


        return map;
    }

    /**
     * 修改时查询修改人的ID
     * @param id 修改人的ID
     * @return
     */
    @RequestMapping("findById")
    @ResponseBody
    public AtsEmpLeaderVo findByid(Integer id){
        // id  用户的ID

        //System.out.println(id);
        AtsEmpLeaderVo ats = leaderService.findById(id);

        System.err.println(ats.getUsName()+"    "+ats.getLeaderName());
        //System.err.println(ats.getCreatTime());

        //System.out.println("修改查询"+ats.getPostName());
        return ats;
    }

    /**
     * 修改某个人的状态
     * @param id 传过来的ID
     * @param value 状态值
     * @return
     */

    @RequestMapping("findByIdswitch")
    @ResponseBody
    public Message findByidswitch(Integer id,Boolean value){
        //System.out.println("id:"+id);
        //创建消息类返回一个消息给前端
        Message msg = new Message();
        //调用修改状态方法
       int cout = leaderService.updateLeaderIsva(id,value);
       if(cout == 1){
            msg.setMsg("修改成功");
        }else{
            msg.setMsg("服务器异常");
        }

        return msg;
    }

    /**
     * 修改某个人的数据
     * @param vo 个人数据
     * @param file 用户头像
     * @return
     */
    @RequestMapping("upLeaderDate")
    @ResponseBody
    public Message updateLeader(AtsEmpLeaderVo vo,MultipartFile file){
        Message msg = new Message();

        System.out.println("进入修改=======================");
        //修改
        int count = leaderService.updateLedaerData(vo,file);
        if(count >0){
            msg.setMsg("修改成功");
        }else{
            msg.setMsg("修改异常，请充值");
        }
        return msg;
    }

    /**
     * 逻辑删除
     * @param id 用户的ID
     * @return
     */
    @RequestMapping("deleteLeader")
    @ResponseBody
    public Message deleteLeader(Integer id){

       int cout = leaderService.deleteLeader(id);

       Message msg = new Message();
       if(cout == 1){
           msg.setMsg("删除成功");
       }else{
           msg.setMsg("删除失败");
       }

        return msg;
    }

    @RequestMapping("deleteCoachDates")
    @ResponseBody
    public Message deleteByIds(String ids){
        Message msg = new Message();
        System.out.println(ids);
        String id[] = ids.split(",");
        int cout =0;
        for (int i=0;i<id.length;i++){
            //将类型转换成int
            Integer e = Integer.valueOf(id[i]);
           cout = leaderService.deleteLeader(e);
        }
        if(cout >0){
            msg.setMsg("删除成功");
        }else{
            msg.setMsg("删除失败");
        }

        return  msg;
    }
}
